package com.example.aqarapp.view.adapter;

import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.aqarapp.entity.BulbInfo;
import com.example.aqarapp.entity.CurtainInfo;
import com.example.aqarapp.entity.DeviceInfo;
import com.example.aqarapp.entity.SocketInfo;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

import aqarapp.R;

public class DeviceRecyclerAdapter extends BaseQuickAdapter<DeviceInfo, BaseViewHolder> {
    private ToggleButton switchTB;

    public DeviceRecyclerAdapter(int layoutResId, @Nullable List<DeviceInfo> data) {
        super(layoutResId, data);
    }

    public DeviceRecyclerAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(@NotNull final BaseViewHolder baseViewHolder, DeviceInfo deviceInfo) {
        switchTB = baseViewHolder.getView(R.id.device_switch_tb);
        if (deviceInfo.getBaseDeviceInfo() instanceof BulbInfo) {
            switchTB.setVisibility(View.VISIBLE);
            baseViewHolder.setBackgroundResource(R.id.device_icon_view, R.mipmap.lightbulb_icon);
            baseViewHolder.setText(R.id.device_name_tv, deviceInfo.DeviceName)
                    .setText(R.id.device_state_tv, ((BulbInfo)deviceInfo.getBaseDeviceInfo()).getBulbLight()+"%")
                    .setText(R.id.device_location_tv, deviceInfo.baseDeviceInfo.location);
            switchTB.setChecked(((BulbInfo) deviceInfo.getBaseDeviceInfo()).getBulbStatus().equals("1")? true : false);
        } else if (deviceInfo.getBaseDeviceInfo() instanceof SocketInfo) {
            switchTB.setVisibility(View.VISIBLE);
            baseViewHolder.setBackgroundResource(R.id.device_icon_view, R.mipmap.socket);
            baseViewHolder.setText(R.id.device_name_tv, deviceInfo.DeviceName)
                    .setText(R.id.device_state_tv, ((SocketInfo)deviceInfo.getBaseDeviceInfo()).getSocketState().equals("1")?"开":"关")
                    .setText(R.id.device_location_tv, deviceInfo.baseDeviceInfo.location);
        } else if (deviceInfo.getBaseDeviceInfo() instanceof CurtainInfo) {
            baseViewHolder.setBackgroundResource(R.id.device_icon_view, R.mipmap.curtain);
            baseViewHolder.setText(R.id.device_name_tv, deviceInfo.DeviceName)
                    .setText(R.id.device_state_tv, ((CurtainInfo)deviceInfo.getBaseDeviceInfo()).getCurtainStatus().equals("1")?"开":"关")
                    .setText(R.id.device_location_tv, deviceInfo.baseDeviceInfo.location);
            switchTB.setVisibility(View.GONE);
        }

        Log.d("liyao5",deviceInfo.baseDeviceInfo.location);
        switchTB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //TODO 判断当前设备是否处于离线状态,若处于离线状态则弹Toast提示网管无响应.
                    //选中
                    Toast.makeText(getContext(), "你选中了switchTB" + (baseViewHolder.getAdapterPosition() + 1), Toast.LENGTH_SHORT).show();
                } else {
                    //未选中
                    Toast.makeText(getContext(), "你取消选中了switchTB" + (baseViewHolder.getAdapterPosition() + 1), Toast.LENGTH_SHORT).show();
                }
            }
        });// 添加监听事件
    }
}
